// 正则环视
const intro = '张三是张三，张三丰是张三丰'
// 向后环视
const res1 = intro.replace(/张三(?!丰)/g, '李四')
const res2 = intro.replace(/张三(?=丰)/g, '李四')

console.log(res1)
console.log(res2)

// 向前环视
const res3 = 'A00 B00'.replace(/(?<=A)00/g, '88')
const res4 = 'A00 B00'.replace(/(?<!A)00/g, '88')

console.log(res3)
console.log(res4)

// 前后环视
const res5 = 'A00B C00D'.replace(/(?<=A)00(?=B)/g, '88')
console.log(res5)

// 数组稳定排序
// sort 排序算法是不稳定排序,在ES2018解决了此问题
// 低版本v8引擎下，sort回调函数比较的值相同时排序可能会错乱

var arr = [
  {id: 1, value: '1'},
  {id: 1, value: '2'},
  {id: 1, value: '3'},
  {id: 1, value: '4'},
  {id: 1, value: '5'},
  {id: 1, value: '6'},
  {id: 1, value: '7'},
  {id: 1, value: '8'},
  {id: 1, value: '9'},
  {id: 1, value: '10'},
  {id: 1, value: '11'},
  {id: 1, value: '12'},
]

var resArr = arr.sort(function(a, b) {
  return a.id - b.id
})
// console.log(resArr)

// reduce 顺序执行promise

function promiseFactor (value) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(value)
    }, 1000)
  })
}

var pArr = [promiseFactor(1), promiseFactor(2), promiseFactor(3)]

var pRes = pArr.reduce((p1, p2) => {
  return p1.then(value => {
    console.log(value)
    return p2
  })
}, Promise.resolve())

pRes.then(value => {
  console.log(value)
})


new Promise((resolve, reject) => {
  resolve(1)
}).then(val => {
  console.log(val)
  Promise.resolve(3).then(val => {
    console.log(val)
  })
  return 2
}).then(val => {
  console.log(val)
})
